package Driver;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

import javax.swing.JOptionPane;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.NoFixedFacet;

import Boundry.Splash;
import Controllers.*;

import Entity.Message;
import Exception.ICMException;

import ICM_OCSF.Client;



public class Driver {
	private static String ip=null;
	private static String port=null;
	private static String LocalNetworkConfigurtaion = "ConfigServerOption.txt";
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{	
		StartUp();
		
	}

	
	public static String getIp() {
		return ip;
	}


	public static void setIp(String ip) {
		Driver.ip = ip;
	}


	public static String getPort() {
		return port;
	}


	public static void setPort(String port) {
		Driver.port = port;
	}


	public static void StartUp (){
		
		Splash splash = new Splash();
		splash.setInfo("Connecting to Server");
		
		try 
		{
			Thread.sleep(1000);
			readDataForConnection();
			int iport = Integer.parseInt(port);
			
			GlobalData.client = new Client(ip,iport);
			GlobalData.client.openConnection();
			
		} catch (Exception e) 
		{
			JOptionPane.showMessageDialog(splash,"Connection Failure", "Error",JOptionPane.ERROR_MESSAGE);
			CloseSplash(splash);
			e.printStackTrace();
			return;
		}
		Initialization();
		CloseSplash(splash);	
		UsersManager.getInstance().login();	
		
	}
	
	
	
	
	private static void CloseSplash(Splash splash) 
	{
		splash.setVisible(false);
		splash.dispose();
	}

	private static void Initialization() 
	{
		GlobalData.guiMangaer = new GuiManager();
		
	}
	
	private static void readDataForConnection() throws ICMException
	{
		Scanner in;
		try 
		{
			in = new Scanner(new FileReader(LocalNetworkConfigurtaion));
		} 
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw new ICMException("Config File Not Found file name: " + LocalNetworkConfigurtaion);
		}
		in.useDelimiter(",");
		while (in.hasNext())
		{
			ip = in.next();
			port =  in.next();
		}
	}

	
		
		
		
}

